﻿using System.Collections.Concurrent;

namespace Hyt.Core.Cache.Providers
{
    public class StaticProvider : ICacheProvider
    {
        private static ConcurrentDictionary<string, object> Storage = new ConcurrentDictionary<string, object>();

        public void Set(string key, object value)
        {
            Storage.AddOrUpdate(key, value,(k,v)=>v);
        }

        public void Set(string key, object value, int expiresSecond)
        {
            Storage.AddOrUpdate(key, value, (k, v) => v);
        }

        public object GetData(string key)
        {
            object result;
            if (Storage.TryGetValue(key, out result))
            {
                return result;
            }
            return null;
        }

        public void Remove(string key)
        {
            object result;
            Storage.TryRemove(key, out result);
        }

        public void RemoveAll()
        {
            Storage = new ConcurrentDictionary<string, object>();
        }
    }
}
